# ZoomAndFadeTransition - Fade and zoom screen transition
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import clutter
from frontend.gui.transitions.transition import Transition

class ZoomAndFadeTransition(Transition):
    """
    FadeTransition 
    
    Fade out all components from the "from_screen" and fade in all components
    of the "to_screen". This class implements simple crossfade effect for
    screen changes.
    """

    def forwardEffect(self, from_screen, to_screen):   
        """ 
        Fade+zoom out all components from the "from_screen" and fade+zoom in all 
        components of the "to_screen".
        @param from_screen: Screen object (Currently displayed screen)
        @param to_screen: Screen object (Screen that is displayed after transition)
        """
        # Initialize to_screen for animation
        to_screen.get_group().set_opacity(0)
        to_screen.get_group().show()
        
        # Fade out current screen
        if from_screen is not None:
            self.fade_out = clutter.Timeline(22, 85)
            self.alpha_out = clutter.Alpha(self.fade_out, clutter.smoothstep_inc_func)
            self.out_fade = clutter.BehaviourOpacity(255, 0, self.alpha_out)
            self.out_fade.apply(from_screen.get_group())
            #from_screen.get_group().set_anchor_point_from_gravity(clutter.GRAVITY_CENTER)
            self.out_zoom = clutter.BehaviourScale(1.0, 1.0, 1.2, 1.2, self.alpha_out)
            #from_screen.get_group().set_anchor_point(0,0)
            self.out_zoom.apply(from_screen.get_group())
        
        # Fade in timeline
        self.fade_in = clutter.Timeline(22, 85)
        self.alpha_in = clutter.Alpha(self.fade_in, clutter.smoothstep_inc_func)
        self.in_behaviour = clutter.BehaviourOpacity(0, 255, self.alpha_in)
        self.in_behaviour.apply(to_screen.get_group())
        #to_screen.get_group().set_anchor_point_from_gravity(clutter.GRAVITY_CENTER)
        self.in_zoom = clutter.BehaviourScale(0.8, 0.8, 1.0, 1.0, self.alpha_in)
        #to_screen.get_group().set_anchor_point(0,0)
        self.in_zoom.apply(to_screen.get_group())
        
        # Start transition animation
        if from_screen is not None:
            self.fade_out.start()
        self.fade_in.start()
    
    def backwardEffect(self, from_screen, to_screen):
        """ 
        Do the same as forwardEffect, but zoom backwards. This gives an illusion
        of going back.
        @param from_screen: Screen object (Currently displayed screen)
        @param to_screen: Screen object (Screen that is displayed after transition)
        """
        # Initialize to_screen for animation
        to_screen.get_group().set_opacity(0x00)
        to_screen.get_group().show()
        
        # Fade out current screen
        self.fade_out = clutter.Timeline(22, 85)
        self.alpha_out = clutter.Alpha(self.fade_out, clutter.smoothstep_inc_func)
        self.out_fade = clutter.BehaviourOpacity(255, 0, self.alpha_out)
        self.out_fade.apply(from_screen.get_group())
        #from_screen.get_group().set_anchor_point_from_gravity(clutter.GRAVITY_CENTER)
        self.out_zoom = clutter.BehaviourScale(1.0, 1.0, 0.8, 0.8, self.alpha_out)
        self.out_zoom.apply(from_screen.get_group())
        self.fade_out.connect('completed', self._remove_from_stage_callback, from_screen)
            
        # Fade in timeline
        self.fade_in = clutter.Timeline(22, 85)
        self.alpha_in = clutter.Alpha(self.fade_in, clutter.smoothstep_inc_func)
        self.in_behaviour = clutter.BehaviourOpacity(0, 255, self.alpha_in)
        self.in_behaviour.apply(to_screen.get_group())
        #to_screen.get_group().set_anchor_point_from_gravity(clutter.GRAVITY_CENTER)
        self.in_zoom = clutter.BehaviourScale(1.2, 1.2, 1.0, 1.0, self.alpha_in)
        self.in_zoom.apply(to_screen.get_group())
        
        # Start transition animation
        if from_screen is not None:
            self.fade_out.start()
        self.fade_in.start()

    def _remove_from_stage_callback(self, timeline, screen):
        """
        Callback for fadeout timeline. This method is called when transition
        direction is BACKWARDS. This method removes old screen from stage.
        """
        self.stage.remove(screen.get_group())